{% extends "rules/base.html" %}
{% load bootstrap3  %}
{% block sidebar %}
    <div class="panel-heading">
        <h2 class="panel-title">Manage users</h2>
    </div>
    <ul class="action panel-body">
        <li>
            <a href="{% url 'list_accounts' %}">Accounts list</a>
        </li>
        <li>
            <a href="{% url 'list_users' %}">User list</a>
        </li>
        <li>
            <a href="{% url 'list_groups' %}">Role list</a>
        </li>
        {% if extra_auth %}
            <li>
                <a data-toggle="tooltip"
                   title="Set priority on Role/Group assignments"
                   href="{% url 'edit_priorities' %}">
                    Edit priorities
                </a>
            </li>
        {% endif %}
    </ul>
    {% if current_action != 'Add user' %}
        <div class="panel-heading">
            <h2 class="panel-title">Actions</h2>
        </div>
        <ul class="action panel-body">
            {% if 'Edit password' not in current_action and not is_from_ldap and user.sciriususer.method == 'local' %}
                <li>
                    <a href="{% url 'edit_password' user.pk %}">Change user password</a>
                </li>
            {% endif %}
            <li>
                <a href="#" onclick="del_user();">Delete user</a>
            </li>
        </ul>
        <script language="JavaScript">
function del_user() {
    let username = {{ username|safe }};
    confirmModal({
        title: `Delete user "${username}"`,
        msg: "Do you want to delete this user ?",
        comment: true,
        cb: function() {
            let comment = $("#generic-modal-comment").val();
            $.ajax({
                type: 'POST',
                data: {'comment': comment},
                url: "{% url 'delete_user' user.pk %}",
                success: function(data) {
                    if (data.redirect) {
                        window.location.href = data.redirect;
                    }

                    if (data.error) {
                        $('#error').parent('.alert').show();
                        $('#error').parent('.alert').text(data.error);
                    }
                }
            });
        }
    });
    return false;
}
        </script>
    {% endif %}
{% endblock sidebar %}
{% block content %}
    <div class="container-fluid">
        <h2>{{ current_action }}</h2>
        <div class="row">
            <div class="col-md-6">
                <form action="{{ request.get_full_path }}" method="post" class="form">
                    {% csrf_token %}
                    {% if 'Edit password' in current_action %}
                        {% bootstrap_form form %}
                    {% else %}
                        {% if form.saml %}
                            {% bootstrap_field form.saml %}
                        {% endif %}
                        {% bootstrap_field form.username %}
                        {% if current_action == 'Add user' %}
                            {% bootstrap_form password_form %}
                        {% endif %}
                        {% bootstrap_field form.first_name %}
                        {% bootstrap_field form.last_name %}
                        {% bootstrap_field form.email %}
                        {% if user.sciriususer %}
                            {% bootstrap_field form.timezone %}
                        {% endif %}
                        {% bootstrap_field form.groups %}
                        {% bootstrap_field form.is_active %}
                        {% if form.no_tenant or form.all_tenants or form.tenants %}
                            <label class="control-label" for="id_tenants">Tenants</label>
                            {% if show_perm_warning %}
                                <div class="help-block">
                                    To access Evebox and/or Kibana, user needs Evebox/Kibana role permissions and "All tenants"/"No tenant" user permissions.
                                </div>
                            {% endif %}
                            {% bootstrap_field form.no_tenant %}
                            {% bootstrap_field form.all_tenants %}
                            {% bootstrap_field form.tenants %}
                        {% endif %}
                    {% endif %}
                    {% buttons %}
                        <button type="submit" class="btn btn-primary">
                            {% bootstrap_icon "ok" %}
                            {% if current_action == 'Add user' %}
                                Add
                            {% else %}
                                Submit
                            {% endif %}
                        </button>
                    {% endbuttons %}
                </form>
            </div>
        </div>
    </div>
    <script language="JavaScript">

$('document').ready(function() {
     $("#id_saml").change(function () {
        if (this.checked) {
            $("#id_password1").parent().hide(duration=200);
            $("#id_password2").parent().hide(duration=200);
        } else {
            $("#id_password1").parent().show(duration=200);
            $("#id_password2").parent().show(duration=200);
        }
    });
});

    {% if form.no_tenant or form.all_tenants or form.tenants %}
function click_all_tenants() {
    if ($("#id_all_tenants").is(":checked")) {
        $('#id_tenants').parents('.form-group').hide(duration=200);
    } else {
        $('#id_tenants').parents('div')[0].style['padding-left'] = '5%';
        $('#id_tenants').parents('.form-group').show(duration=200);
    }
}

$('document').ready(function() {
    $("#id_all_tenants").change(click_all_tenants);
    click_all_tenants();
});
    {% endif %}
    </script>
{% endblock %}
